//jquery
$(document).ready(function(e) {
    $.backstretch([
      "../public/images/asset/1.jpg",
      "../public/images/asset/2.jpg",
      ], {
        fade: 750,
        duration: 4000
    });
    $("#click-reset-pass").click(function(){
        $("#login-frm").hide();
        $("h5").hide();
        $("#fogot-pass-frm").fadeIn(500);
    })
    $(".back").click(function(){
        $("#fogot-pass-frm").hide();
        $("h5").fadeIn();
        $("#login-frm").fadeIn(500);
    })
    var kq;
            if(mga_checkCookie("c_mga_sp_in_page")==true)
			{
				kq=mga_getCookie("c_mga_sp_in_page");
			}else
			{
				mga_setCookie("c_mga_sp_in_page",20,1);
				kq=20;
			}
			$("#mga_pt_slsp").val(kq);
	
});//end jquey
// javasript
    function CheckAll(source) {
            checkboxes = document.getElementsByName('check-product');
            for(var i=0, n=checkboxes.length;i<n;i++) {
            checkboxes[i].checked = source.checked;
            }
        }
        
    $(document).on('click', '#btn_TenWebsite', function(){ 
     $(this).addClass("btn-warning");
 });
        
$(".btn-config").click(function(o){
    var a =$(this).attr("rel");
    $("input[name="+a+"]").removeAttr('disabled');
   // $("button[name=btn_"+a+"]").html("cập nhật");
   $(this).html("cập nhật")
    $(this).removeClass("btn-primary");
    $(this).removeClass("btn-config");
    $(this).addClass("btn-success");
    $(this).addClass("btn-update-config");
    $(this).attr("name","btn_"+a);
   
})



    if($("#btn_Tagline").click(function(){
        alert("tagline dc click");
    }))

        
// end javasript
//mga sript
//buton click chose file
//cookied
//tạo cookie 
function mga_setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString() +"; path=/");
document.cookie=c_name + "=" + c_value;
}

//lấy cookie
function mga_getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
  {
  c_start = c_value.indexOf(c_name + "=");
  }
if (c_start == -1)
  {
  c_value = null;
  }
else
  {
  c_start = c_value.indexOf("=", c_start) + 1;
  var c_end = c_value.indexOf(";", c_start);
  if (c_end == -1)
  {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
//kiem tra cookie trả về ture or false
function mga_checkCookie(name_Cookie)
{
	var check=mga_getCookie(name_Cookie);
	  if (check!=null && check!="")
	  {
		return true
	  }
	else 
	  {
		return false
	  }
}

//hàm chỉ cho nhập số 0->9 bắt onkeypress='Onkey_number ()'							
function mga_onkey_number (evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
   var regex = /[0-9]/;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
//cut " "
function mga_ignoreSpaces(string) {
var temp = "";
string = '' + string;
splitstring = string.split("&nbsp;");
for(i = 0; i < splitstring.length; i++)
temp += splitstring[i];
return temp;
}
//format number
function mga_number_format(number, decimals, dec_point, thousands_sep) {
  number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function (n, prec) {
      var k = Math.pow(10, prec);
      return '' + Math.round(n * k) / k;
    };
  // Fix for IE parseFloat(0.55).toFixed(0) = 0;
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  if (s[0].length > 3) {
    s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0');
  }
  return s.join(dec);
}
//ep kieu str qua num
function mga_str2num(val)
{
                val = '0' + val;
                val = parseFloat(val);
                return val;
}
//mga jquery
$(document).ready(function(e) {
 	//get element hover show img
      $('img[id^=mga_img_hover_admin_product_]').hover(function(e) {
		  var arr=$(this).attr("id").split("_");//cut get id product
		  var id=arr[arr.length-1];// id product
		  var element_show="#mga_change_img_hover_admin_product_"+id;// get id element show		
		  var url=$(this).attr("src");//get url
		  	$(element_show).show();
		 	$(element_show).attr("src",url);// show			
        },function(e){//hover end
			  var arr=$(this).attr("id").split("_");//cut get id product
			  var id=arr[arr.length-1];// id product			 
			  var element_show="#mga_change_img_hover_admin_product_"+id;// get id element show	
			  	$(element_show).hide();
				$(element_show).attr("src","");// hiden		
			});
			
		//format 100,000,000
		$('#mga_ipt_ist_gia_admin_product').keyup(function(e) {
            $(this).val(mga_number_format($(this).val()));
        });
			//click vào icon text show ckediter load dử liệu có sẳn icon text
		//click ok ckediter lấy dử liệu bỏ vào cái nút icon text
		//click show
		$('td[id^=mga_td_show_edt_admin_product_]').click(function(){
			//get id
			var arr=$(this).attr("id").split("_");
				id=arr[arr.length-1];	// id insert ==new // update = number
			var cl=arr[arr.length-2]; //name colum mt or kt or hd
			var ttr="#mga_ttr_admin_product_"+cl+"_"+id; // get id textarea
			//set val button
			$('#mga_btn_ok_edt_admin_product').val("#mga_ttr_admin_product_"+cl+"_"+id);
			//show edt
			$('#mga_div_edt_admin_product').show();//show edt
			
				//set name edt
					if(cl=="mt")
					{
						name="Mô tả";
					}
					
					if(cl=="kt")
					{
						name="Kích thước";
					}
					
					if(cl=="hd")
					{
						name="Hướng dẩn";
					}
					
					//set val ckeditor
					var txt=$(ttr).val();				
					CKEDITOR.instances.mga_ttr_edt_admin_product.editable().setHtml(txt);
					
				if(id=="new")//insert	
				{									
					//set name edt
					$('#mga_spn_name_edt_admin_product').html("Thêm mới - "+name);
					
					//click button ok
					$('#mga_btn_ok_edt_admin_product').click(function(e) {	
						//set new txt 
					var txt=CKEDITOR.instances.mga_ttr_edt_admin_product.getData();						
						//set new txt					
						$($(this).val()).val(txt);
						//close edt
						$('#mga_div_edt_admin_product').hide();	
                    });
												
				}
				else
				{					
					$('#mga_spn_name_edt_admin_product').html($(this).attr("n_pro")+" - "+name);
					//update 
					//set val ckeditor
					//CKEDITOR.instances.mga_ttr_edt_admin_product.editable().setHtml(txt);
					//khi bam button ok
					//get val ckeditor	
					//var ckd = CKEDITOR.instances['mga_ttr_edt_admin_product'].getData();
					$('#mga_btn_ok_edt_admin_product').click(function(e) {							
						//set new txt 
					var txt=CKEDITOR.instances.mga_ttr_edt_admin_product.getData();						
						//set new txt					
						$($(this).val()).val(txt);
						//close edt
						$('#mga_div_edt_admin_product').hide();	
                    });
				}
				
			});
			
		//click close "huy"
		$('#mga_btn_huy_edt_admin_product').click(function(){
			$('#mga_div_edt_admin_product').hide();			
			});
		//insert product admin
		$("#mga_btn_int_prd_admin").click(function(e) {
			//get val
            var name=$("#txt_int_name_product").val();
			var menu=$("#slc_int_menu_product").val();			
			var unt=$("#slc_int_unit_product").val();
			var gia=$("#mga_ipt_ist_gia_admin_product").val();
			var mt=$("#mga_ttr_admin_product_mt_new").val();
			var kt=$("#mga_ttr_admin_product_kt_new").val();
			var hd=$("#mga_ttr_admin_product_hd_new").val();
			//fomat gia nuber
			var arr=gia.split(",");
				gia=0;
			for(var i=0;i<=arr.length;i++)
			{
				gia+=arr[i];
			}
			gia=mga_str2num(gia);			
			//split array menu
			var arr=menu.split("_");
			var menu_id=arr[0];
			var menu_kc=arr[1];
			//set error
			var er=null;
					
			if(name=="" || name==null) // kiểm tra txt name product
			{
				//doi mau boder
				$("#txt_int_name_product").css("border", "#F71F1F solid 1px");
				er=1;	//co er
			}
			
			//ktphan loai
			if(menu_kc=="k")
			{
				//doi mau boder
				$("#slc_int_menu_product").css("border", "#F71F1F solid 1px");
				er=1;	//co er
			}
			
			//kiem tra gia
			if(gia=="" || gia==null || gia<=0)
			{
				//doi mau boder
				$("#mga_ipt_ist_gia_admin_product").css("border", "#F71F1F solid 1px");
				er=1;	//co er
			}
			//nếu không xuất hiện lổi thuc hien ajax
			if(er!=1)
			{
				var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_product/insert.php",
					data:{	j_name:name,
							j_menu:menu_id,
							j_unt:unt,
							j_gia:gia,
							j_mt:mt,
							j_kt:kt,
							j_hd:hd,
							},					
						});
		//callback			
				rq.done(function(msg){				
						if(msg=="")//insert = t
						{
						 	 window.location.reload();//load lai trang
						}
					});
		
			}//end không xuất hiện lổi thuc hien ajax
        });//end insert product admin
			
		//onkey up kt er name
		$("#txt_int_name_product").keyup(function(e) {           
			if($(this).val()==""||$(this).val()==null)
			{
				$(this).css("border", "#F71F1F solid 1px");
			}
			else
			{
				$(this).css("border", "#ccc solid 1px");
			}
        });
		//onkey up kt er gia
		$("#mga_ipt_ist_gia_admin_product").keyup(function(e) {
            if($(this).val()==""||$(this).val()==null||$(this).val()<=0)
			{
				$(this).css("border", "#F71F1F solid 1px");
			}
			else
			{
				$(this).css("border", "#ccc solid 1px");
			}
        });
		//onkey up kt er menu
		$("#slc_int_menu_product").change(function(e) {
          	var arr=$(this).val().split("_");			
			if(arr[1]=="k")
			{
				$(this).css("border", "#F71F1F solid 1px");
			}
			else
			{
				$(this).css("border", "#ccc solid 1px");
			}	
        });
		//show hide input update
		$("tr[id^=mga_hover_tr_ad_prt_]").hover(function(){
			 var arr=$(this).attr("id").split("_");
			 var id=arr[arr.length-1];
			 var id_show="#mga_spn_ad_prt_"+id;
			 var id_hide="#mga_ad_prt_ud_"+id;
			 $(id_show+"_ten").hide();
			 $(id_show+"_loai").hide();
			 $(id_show+"_dv").hide();
			 $(id_show+"_gia").hide();
			 $(id_hide+"_ten").show();
			 $(id_hide+"_loai").show();
			 $(id_hide+"_dv").show();
			 $(id_hide+"_gia").show();
			},function(){
				 var arr=$(this).attr("id").split("_");
				 var id=arr[arr.length-1];
				 var id_show="#mga_spn_ad_prt_"+id;
				 var id_hide="#mga_ad_prt_ud_"+id;
				
				 $(id_show+"_ten").show();
				 $(id_show+"_loai").show();
				 $(id_show+"_dv").show();
				 $(id_show+"_gia").show();
				 $(id_hide+"_ten").hide();
				 $(id_hide+"_loai").hide();
				 $(id_hide+"_dv").hide();
				 $(id_hide+"_gia").hide();				 
				 //up tt pro no img
				 //get old val
				
				 	var old_ten=$(id_show+"_ten").html();
					var old_loai=$(id_show+"_loai").html();
					var old_dv=$(id_show+"_dv").html();
					var old_gia=$(id_show+"_gia").html();
					var old_mt=$(id_show+"_mt").val();
					var old_kt=$(id_show+"_kt").val();
					var old_hd=$(id_show+"_hd").val();
				//get new val 
				
					var new_ten=$(id_hide+"_ten").val();
					var new_loai=$(id_hide+"_loai option:selected").text();
					var new_dv=$(id_hide+"_dv option:selected").text();
					var new_gia=$(id_hide+"_gia").val();					
					var new_mt=$("#mga_ttr_admin_product_mt_"+id).val();
					var new_kt=$("#mga_ttr_admin_product_kt_"+id).val();
					var new_hd=$("#mga_ttr_admin_product_hd_"+id).val();
					// if val() = new => update
					//kiểm tra dử liệu rổng
				
					if(old_ten!=new_ten||old_loai!=new_loai
					 ||old_dv!=new_dv||old_gia!=new_gia
					 ||old_mt!=new_mt||old_kt!=new_kt
					 ||old_hd!=new_hd)
					{						
					//set ajax update
					var new_id_loai=$(id_hide+"_loai").val();
					var arr=new_id_loai.split("_");
						new_id_loai=arr[0];
					var new_id_dv=$(id_hide+"_dv").val();									
					var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_product/update.php",
					data:{	j_id:id,
							j_ten:new_ten,
							j_gia:new_gia,
							j_mt:new_mt,
							j_kt:new_kt,
							j_hd:new_hd,
							j_id_loai:new_id_loai,
							j_id_dv:new_id_dv,
										},					
						});
					//callback			
					rq.done(id,new_ten,new_loai,new_dv,new_gia,function(msg){						
							if(msg=="ok")
							{
								var id_show="#mga_spn_ad_prt_"+id;
								 $(id_show+"_ten").html(new_ten);
								 $(id_show+"_loai").html(new_loai);
								 $(id_show+"_dv").html(new_dv);
								 $(id_show+"_gia").html(new_gia);
							}		
						});
					}else{}				
				});
	
			//fomat num up date
			$('.mga_ud_format_num').keyup(function(e) {
            $(this).val(mga_number_format($(this).val()));
       		});
			//key up err update		
			$("input[id^=mga_ad_prt_ud_]").keyup(function(e) {
                 if($(this).val()==""||$(this).val()<=0)
				   {
					   $(this).css("border", "#F71F1F solid 1px");
					}
					else
					{
						$(this).css("border", "#ccc solid 1px");
					}			
            });
		
			//change input update    		 
			$("input[id^=mga_ipt_up_img_]").change(function(){		 	
			//lấy file					
			var data = new FormData();
			jQuery.each($(this)[0].files, function(i, file) {
				data.append('file-'+i, file);				
			});//end lay file
			// set data file
				//set cookie mutifile
				//setCookie('C_sl_img_up',i,1);
				var arr=$(this).attr("id").split("_");
					id=arr[arr.length-2]+"_"+arr[arr.length-1];	
					//load img
					$("#mga_img_hover_admin_product_"+id).attr("src","../public/images/admin/ajax-loader-blue.gif");			
			//ajax
			var rq=$.ajax({
						type: "POST",
						url: '../public/js/ajax/admin_product/up_img.php?id='+id+'',
						data: data,						
						cache: false,				
						contentType: false,
						processData: false,	
							});
			//callback			
			rq.done(function(msg){						
							$("#mga_img_hover_admin_product_"+msg).attr("src","../public/images/product/mga_prt/min_"+msg+".jpg");						
					});
		});//end change av
		//phan trang
	 	$("#mga_pt_slsp").change(function(e) {           
			 mga_setCookie("c_mga_sp_in_page",$(this).val(),1);
			 window.location.reload();//load lai trang
        });	
	//delete product
    $("button[id^=mga_delete_]").click(function(e) {		
		var id=$(this).val();
		$("#mga_hover_tr_ad_prt_"+id).html("");	//delete html
		//ajax delete img and data	
		var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_product/delete.php",
					data:{	j_id:id,},					
						});
		//callback			
				rq.done(id,function(msg){});		
    });
	//banner
	  $("input[id^=mga_input_banner_]").change(function(e) {		  	
            var arr=$(this).attr("id").split("_");
			var id=arr[arr.length-1];
			$("#mga_img_banner_"+id).attr("src","../public/images/admin/big_load.gif");
			//lấy file					
			var data = new FormData();
			jQuery.each($(this)[0].files, function(i, file) {
				data.append('file-'+i, file);				
			});//end lay file
			var rq=$.ajax({
						type: "POST",
						url: '../public/js/ajax/admin_banner/up_img.php?id='+id+'',
						data: data,						
						cache: false,				
						contentType: false,
						processData: false,	
							});
			//callback			
			rq.done(function(msg){						
							$("#mga_img_banner_"+msg).attr("src","../public/images/banner/"+msg+".jpg");
                            $(document).load();											
					});
        });
			//onload			
     	$("#mga_slt_list_pro_0").ready(function(e) {
            var id=0; // 1
			var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_list_pro/slt_all.php",
					data:{j_id:id,},					
						});
		//callback			
			rq.done(function (msg){
				 $("#mga_slt_list_pro_0").html($("#mga_slt_list_pro_0").html()+msg)
				 });
        });		
		//select
		$("select[id^=mga_slt_list_pro_]").change(function(e) {			
            var arr=$(this).attr("id").split("_");
			var vt=arr[arr.length-1];
			var id=$(this).val();
			mga_setCookie("mga_id_list_pro",id,1);
			//alert(mga_getCookie("mga_id_list_pro"));			
			$('#mga_name_tm').val($('#mga_slt_list_pro_'+vt+' option:selected').text());
			$('#mga_name_tm').attr("lv",id);
			$('#mga_name_tm').attr("vt",vt);
			if(id!="g")
			{
					var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_list_pro/slt_all.php",
					data:{j_id:id,},					
						});
		//callback			
			rq.done(vt,function (msg){
				if(vt<3)
				{					
					vt=mga_str2num(vt)+1;					
					$("#mga_slt_list_pro_"+vt).html(msg);
				}
				 });				
			}		
        });
		//update list pro
		$('#btn_ud_list_pro').click(function(e) {
            var ten=$('#mga_name_tm').val();
			var id=$('#mga_name_tm').attr('lv');
			var vt=$('#mga_name_tm').attr('vt');
			//ajax
			var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_list_pro/update_list_pro.php",
					data:{j_id:id,j_name:ten},					
						});
		//callback			
			rq.done(vt,function (msg){
				$(vt).text(msg);
				 });				
        });
		//insert list pro
		$('#btn_int_list_pro').click(function(e) {
            var ten=$('#mga_name_tm').val();
			var id=$('#mga_name_tm').attr('lv');
			var g=id;//thu muc goc		
				if(id=="g")
				{
					id=0;
				}			
			var vt=$('#mga_name_tm').attr('vt');
			//ajax
			if(ten!=""||vt<3)
			{
				var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_list_pro/insert_list_pro.php",
					data:{j_id:id,j_name:ten},					
						});
		//callback			
				rq.done(ten,g,id,vt,function (msg){
					if(g=="g")
					{
						vt=0;
					}
					else
					{
						vt=mga_str2num(vt)+1;
					}					
					$("#mga_slt_list_pro_"+vt).html(msg+$("#mga_slt_list_pro_"+vt).html());
				 });				
			}
			
        });
		//delete list pro
		$('#btn_del_list_pro').click(function(e) {
            var ten=$('#mga_name_tm').val();
			var id=$('#mga_name_tm').attr('lv');
			var g=id;//thu muc goc
			if(id=="g")
				{
					id=0;
				}					
			var vt=$('#mga_name_tm').attr('vt');
			//ajax
			if(g!="g")
			{
				var rq=$.ajax({
					type: "POST",
					url: "../public/js/ajax/admin_list_pro/delete_list_pro.php",
					data:{j_id:id,j_name:ten},					
						});
		//callback			
			rq.done(ten,g,id,vt,function (msg){
					
						var arr=$("#mga_slt_list_pro_"+vt).html();
						arr=arr.split('<option value="'+id+'">'+ten+'</option>');
						$("#mga_slt_list_pro_"+vt).html(arr[0]+arr[1]);
												
				//window.location.reload();
				 });				
			}
			
        });
		
});
